/**
 * @author banglong
 * @date 2016/12/21
 */
require(['bus', 'Layer', 'tools', 'billListApi'], function (bus, layer, tools, api) {

  // 检查是否账册是否需要 判断余额
  bus.bind('billlist.checkGoodsBook', function (event, payload) {
    api.getAccountBalance({
      className: 'ChargeBillManager',
      method: 'GetAccountBalance',
      deptAccountID: payload.param.CurrentDeptID
    }).then(function (jsonResult) {

      if (jsonResult.issuccess !== 0) {
        Promise.reject('获取信息失败')
      }
      var balance = JSON.parse(jsonResult.result).Balance;
      var isCredit = parseInt(balance) > 0;
      var icon = isCredit ? 6 : 2;
      var prompt = isCredit ? '您的当前余额为：' + balance + '元' : '当前余额为：' + balance + '元，' + '账户没有余额，请先去财务科充值';
      var index = layer.confirm(prompt, {
        icon: icon,
        skin: 'layer-ext-moon',
        btn: ['确定'] //按钮
      }, function () {
        layer.close(index);
        if (isCredit) {
          if (payload.callback) {
            payload.callback()
          }
        } else {
          if (payload.errorCallback) {
            payload.errorCallback()
          }
        }
      });
    })['catch'](function (ex) {
      tools.alterError(ex)
    })
  });

  // 发送和作废
  bus.bind('billlist.sendAndNullifyBill', function (event, payload) {
    api.nullifyDrawBill({
      className: payload.className,
      method: payload.method,
      billSN: payload.bill.BillSN,
      userID: payload.userID
    }).then(function (jsonResult) {
      if (jsonResult.issuccess === 0) {
        // bill.StatusStr = status;
        bus.triggerHandler('billlist.refreshBillsTable'); // 刷新表格
      }else{
    	 tools.alterError( jsonResult);
      }
    })['catch'](function (ex) {
      tools.handleError(ex);
    });
  });

  // 首先加载供应科室的信息 todo
  bus.bind('billlist.getProvider', function (event, payload) {});
});